package cn.yujiago.miaosha.exception;

import cn.yujiago.miaosha.dto.CodeMsg;
import cn.yujiago.miaosha.dto.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    private final static Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(UserException.class)
    public Result handlerUserException(UserException e){
        LOG.info("UserException handler " + e.getMessage());
        return Result.error(e.getCodeMsg());
    }

    @ExceptionHandler(MiaoshaException.class)
    public Result handlerMiaoshaException(MiaoshaException e){
        LOG.info("UserException handler " + e.getMessage());
        return Result.error(e.getCodeMsg());
    }

    @ExceptionHandler(Exception.class)
    public Result handlerException(Exception e){
        LOG.info("Exception handler " + e.getMessage());
        return Result.error(CodeMsg.SERVER_ERROR);
    }

}
